#!coding=utf-8
from standard_function import *

def del_appdown_task(driver):
    #清空下载任务
    try:
       btn = WaitforElementById(driver,'com.mappn.gfan:id/updateBtn',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    try:
       btn = WaitforElementByName(driver,'任务',30)
    except NoSuchElementException:
        raise NoSuchElementException
    else:
        btn.click()

    while True:
        try:
            tasks=WaitforElementsById(driver,"com.mappn.gfan:id/installedInfoView",30)
        except NoSuchElementException:
            break
        else:
            tasks[0].click()
            try:
                clear_btn=WaitforElementById(driver,'com.mappn.gfan:id/uninstallBtn',30)
            except NoSuchElementException:
                driver.back()
                continue
            else:
                clear_btn.click()
            time.sleep(2)
    driver.back()

def clickappdown(driver):
    try:
        list_f=WaitforElementById(driver,'com.mappn.gfan:id/recyclerView',30)
    except NoSuchElementException:
        raise NoSuchElementException

    try:
        down_btns=WaitforElementsByClass(list_f,'android.widget.LinearLayout',30)
    except NoSuchElementException:
        raise  NoSuchElementException
    else:
        print len(down_btns)
        for item in down_btns:
            try:
                WaitforElementById(item,'com.mappn.gfan:id/appNameText',5)
            except NoSuchElementException:
                continue
            try:
                size=WaitforElementById(item,"com.mappn.gfan:id/sizeText",30).get_attribute("text")
            except NoSuchElementException:
                continue
            print size
            if 'G' not in size:
                if 'K' in size or float(size[0:-1])<10:
                    continue
            try:
                down_btn=WaitforElementById(item,'com.mappn.gfan:id/actionText',5)
            except NoSuchElementException:
                continue
            if down_btn.get_attribute('text')==u'下载':
                down_btn.click()
                waittime=20
                while waittime:
                    try:
                        text=WaitforElementById(item,'com.mappn.gfan:id/downSizeText',5).get_attribute("text")
                    except NoSuchElementException:
                        pass
                    print text
                    if '0/' not in text:
                        down_btn.click()
                        break
                    else:
                        time.sleep(0.5)
                        if waittime==0:
                            down_btn.click()
                            break
                        waittime=waittime-1
                print "----------------------------------------------"
    #driver.back()



def soft(driver):
    categorys = [u'实用工具',u'聊天社区',u'输入法',u'浏览器',u'网络购物',u'交通导航',u'教育阅读',u'生活娱乐',u'拍照摄影',u'主题桌面', \
                 u'系统工具',u'安全防护',u'词典翻译',u'通话通信',u'资讯新闻',u'健康医疗',u'影音播放',u'金融理财',u'电子办公',u'体育竞技']
    try:
        WaitforElementByName(driver,u'应用',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            driver.back()
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def game(driver):
    categorys = [u'动作游戏',u'冒险游戏',u'策略游戏',u'街机游戏',u'RPG游戏',u'休闲游戏',u'棋牌游戏',u'模拟游戏',u'竞速体育',u'小游戏']
    try:
        WaitforElementByName(driver,u'游戏',30).click()
        WaitforElementByName(driver,u"分类",30).click()
    except NoSuchElementException:
        raise  NoSuchElementException
    time.sleep(2)
    swipeUp(driver,0.9,random.randint(0,8)*0.1)
    time.sleep(2)
    for i in categorys:
        print "Current category is:",i
        try:
            btn=WaitforElementByName(driver,i,5)
        except NoSuchElementException:
            driver.back()
            continue
        else:
            btn.click()
            try:
                clickappdown(driver)
            except NoSuchElementException:
                continue
            driver.back()

def run():
    desired_caps = {}
    #desired_caps['automationName'] = 'uiautomator2'
    desired_caps['platformName'] = "Android"               # 声明是ios还是Android系统
    desired_caps['platformVersion'] = ANDROID_VERSION              # Android内核版本号，也可以在模拟器设置中查看
    desired_caps['deviceName'] = DEVICE         # 连接的设备名称，可通过adb devices查看
    desired_caps['appPackage'] = 'com.mappn.gfan'    # apk的包名
    desired_caps['appActivity'] = 'com.gfan.gm3.home.HomeActivity'          # apk的launcherActivity
    desired_caps['noReset']='true'                         #不需要再次安装apk
    driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)          # 建立 session
    time.sleep(3)
    #driver.find_element_by_name().tag_name
    del_appdown_task(driver)
    soft(driver)
    time.sleep(2)
    driver.back()
    game(driver)
    driver.quit()

#run()